//app.js
const promise = require("utils/promise.js");
var wh = require("utils/wh.js");
App({
  cover_img: '',
  is_user_save_data: 0,
  // 微信登录
  wlogin: function (userInfo) {
    let that = this;
    wx.getExtConfig({
      success: function (res) {
        module.exports.globalData = res.extConfig;
        //进行微信登录
        wx.login({
          success: function (res) {
            if (res.code) {
              let data = { code: res.code, encryptedData: userInfo.encryptedData, iv: userInfo.iv, rawData: userInfo.rawData, signature: userInfo.signature };
              wh.Post('/api/user/login', data, function (info) {//登录成功返回的信息
                let userinfo = JSON.stringify(info);
                wx.setStorageSync('W_Userinfo', userinfo);//存储
                wx.setStorageSync('employId', info.id);
                wx.setStorageSync('whlw_app_id', module.exports.globalData.app_id);
                //由于这里是网络请求，可能会在 Page.onLoad 之后才返回 所以此处加入 callback 以防止这种情况
                if (that.employIdCallback) {
                  that.employIdCallback(info.id);
                }
                return true;
              });
            }
          },
          fail: function () {
            return false;
          }
        })
      }
    });
  },
  app_auth: function (self) {
    self.selectComponent("#auth").onCheck()
    let mobile_validate = wx.getStorageSync('mobile_validate')
    let employId = wx.getStorageSync('employId')
    let that = self
    if (employId != '' && employId != undefined) {
      if (mobile_validate != 1) {
        let personUrl = '/api/shop.ajax/person_info';
        wh.Post(personUrl, '', function (res) {
          that.selectComponent("#mobile_auth").onCheckMobile(res.mobile_validated)
        });
      } else {
        that.selectComponent("#mobile_auth").onCheckMobile(1)
      }
    }
  },
  is_user_save: function () {
    let postUrl = '/api/madetoorder.decorate.index/get_operate_user_info',
      that = this,
      params = {
        is_submit: 0
      };
    wh.Post(postUrl, params, function (res) {
      that.is_user_save_data = res;
    })
  },
  /**
   * 封面图
   */
  get_page_cover: function (dc_type) {
    let postUrl = '/api/madetoorder.decorate.index/get_page_cover',
      that = this,
      postData = {
        type: dc_type
      };

    return new Promise(function (resolve, reject) {
      wh.Post(postUrl, postData, function (res) {
        resolve(res);
      });
    });
  },
});
